package com.wys.ws_im.service;

import com.wys.ws_im.dao.MessageDao;
import com.wys.ws_im.dao.OfflineMessageDao;
import com.wys.ws_im.dao.UserListDao;
import com.wys.ws_im.entity.Message;
import com.wys.ws_im.entity.OfflineMessage;
import com.wys.ws_im.entity.UserList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息处理
 *
 * @author wys
 * created in 0:33 2019/4/16
 */
@Slf4j
@Service
public class MessageService {

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private OfflineMessageDao offlineMessageDao;

    @Autowired
    private UserListDao userListDao;

    /**
     * 打开聊天窗口时，查看获取的消息是离线的（未读的）
     * 查看最新的20条消息
     * 还是已读的消息
     *
     * 查看消息 查看未读的和已经读的
     * 1.有离线的   获取离线 + 20
     * 2.没有   直接20
     * 减20个
     * 区分离线问题
     *
     * @param sendId
     * @param ulid
     * @return
     */
    public List<Message> getOnOpenMessage(Integer sendId, Integer ulid) {

        List<Message> messages;

        log.info(" sendId{}  ulid{} ", sendId, ulid);

        // 离线消息实体
        OfflineMessage single = offlineMessageDao.createLambdaQuery()
                .andEq(OfflineMessage::getAid, sendId)
                .andEq(OfflineMessage::getUlid, ulid)
                .single();

        log.info("single {}",single);
        if (single == null) {
            messages = messageDao.getMessages(ulid);
            return messages;
        }
        // 没有离线消息
        if (single.getCount() == 0) {
            messages = messageDao.getMessages(ulid);
        }
        // 有
        // 获取了  就把数量设置为0
        else {
            messages = messageDao.getOfflineMessages(single.getCount() + 20, ulid);
            single.setCount(0);
            log.info("single {}",single);
            offlineMessageDao.updateById(single);
        }

        return messages;
    }

    /**
     * 存消息
     * @param message 消息
     */
    public void saveMessage(Message message) {
        // 更新聊天室时间
        UserList userList = userListDao.createLambdaQuery()
                .andEq(UserList::getId, message.getUlid())
                .single();
        userList.setUpdatedTime(message.getSendTime());
        try {
            messageDao.insert(message, true);
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 存离线消息 == >  对方存
     * @param message
     */
    public void saveOfflineMessage(Message message) {
        // 更新聊天室时间
        UserList userList = userListDao.createLambdaQuery()
                .andEq(UserList::getId, message.getUlid())
                .single();
        userList.setUpdatedTime(message.getSendTime());

        // 离线
        OfflineMessage offlineMessage = offlineMessageDao.createLambdaQuery()
                .andEq(OfflineMessage::getUlid, message.getUlid())
                .andEq(OfflineMessage::getAid, message.getReceiveId())
                .single();
        // 先存了消息 获取到最后一条消息id
        messageDao.insert(message, true);
        Integer lastReadMsgId = messageDao.getLastReadMsgId(message.getUlid(), message.getSendId());

        // 不存在离线，添加离线
        if (offlineMessage == null) {
            log.info("121 // 不存在离线，添加离线 {} ", offlineMessage);
            OfflineMessage offlineMessage1 = new OfflineMessage();
            offlineMessage1.setUlid(message.getUlid());
            offlineMessage1.setAid(message.getReceiveId());
            offlineMessage1.setCount(0);
            // 先存了消息  再获得消息id
            offlineMessage1.setLastReadMessageid(lastReadMsgId);
            offlineMessageDao.insert(offlineMessage1, true);
        }
        // 存在离线，修改离线
        else {
            offlineMessage.setCount(offlineMessage.getCount() + 1);
            offlineMessage.setLastReadMessageid(lastReadMsgId);
            offlineMessageDao.updateById(offlineMessage);
        }

    }

}
